package com.news.news.service;

import com.news.news.dao.LoginDao;
import com.news.news.modle.Admin;
import com.news.news.util.JWTUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * Created by 罗铠威 on 2023/8/29 16:21
 * type:
 */
@Service
@Transactional
public class LoginService {

    @Resource
    LoginDao loginDao;

    public Admin login(Admin admin){
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        admin.setPassword(password);
        Admin login = loginDao.login(admin);
        if(login!=null){
            String adminToken = JWTUtil.getToken(login);
            login.setAdminToken(adminToken);
        }
        return login;
    }

    public String testpassword(String curpwd, String token) {
        String password = DigestUtils.md5DigestAsHex(curpwd.getBytes());
        Integer id=JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        return loginDao.testpassword(password,id);
    }
//

    public void updatepassword(String password, String token) {
        String newpassword = DigestUtils.md5DigestAsHex(password.getBytes());
        Integer id=JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        loginDao.updatepassword(newpassword,id);
    }
}
